﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.BLL.Project;
using Jzy.Model.Project;
using Jzy.Core.Common.Web;
using Frame.Lib.Utilities;
using Frame.Lib.Common;
using Frame.Lib.Enums;
using Frame.Lib.Core.Dict;


namespace Jzy.Web.Project.BugManage
{
    public partial class Detail : PageDetail
    {
        private FromPage ComeFrom
        {
            get
            {
                return (FromPage)int.Parse(Request["FromPage"]);
            }
        }

        private PrjBugBO _bug = new PrjBugBO();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DispInfo();
            }
            AddAttr();
            AllowPageScroll();
        }

        private void AddAttr()
        {
            JSUtil.ClickBtn(lnkDo, btnDo);
            JSUtil.ClickBtn(lnkChangeReceiver, btnChangeReceiver);
            JSUtil.ClickBtn(lnkClose, btnClose);
            JSUtil.ClickBtn(lnkCancel, btnCancel);

            btnClose.Attributes.Add("onclick", "return confirm('一旦关闭，表示该缺陷已修改完毕，确认？');");

            string strEncryptID = StrUtil.EncryptUrl(PKID);
            string strUrl = GlobalVar.WebCurrPath + "DoBug.aspx?ID=" + strEncryptID;
            JSUtil.ShowForm(btnDo, "处理缺陷", null, strUrl, 400, 230);

            strUrl = GlobalVar.WebCurrPath + "ChangeReceiver.aspx?ID=" + strEncryptID;
            JSUtil.ShowForm(btnChangeReceiver,"重新指派缺陷处理人", null, strUrl, 400, 200);
        }

        private void DispInfo()
        {
            PrjBugInfo info = _bug.GetInfo(PKID);
            EntityUtil.SetControl(pnlMain, info);

            if (info.PlanEndTime == DateTime.MinValue)
            {
                PlanEndTime.Text = "";
            }
            else
            {
                PlanEndTime.Text = info.PlanEndTime.ToString("yyyy-MM-dd");
            }

            if (info.RealEndTime == DateTime.MinValue)
            {
                RealEndTime.Text = "";
            }
            else
            {
                RealEndTime.Text = info.RealEndTime.ToString("yyyy-MM-dd");
            }

            Priority.Text = ((BugPriority)info.Priority).ToString();
            Severity.Text = ((BugSeverity)info.Severity).ToString();
            Status.Text = ((BugStatus)info.Status).ToString();
            Remark.Text = info.Remark.Replace("\r\n", "<br/>");
            _bug.SetColorByStatus(Status, (BugStatus)info.Status);         

            PrjName.Text = (new ProjectBO()).GetName(info.PrjID);
            TypeName.Text = (new DictBO()).GetName(DictItem.缺陷类型, info.TypeCode);

        
            BugStatus bugStatus = (BugStatus)info.Status;
            switch (bugStatus)
            {
                case BugStatus.已解决:
                    liClose.Visible = btnClose.Visible = true;
                    liDo.Visible = btnDo.Visible = false;
                    liChangeReceiver.Visible = btnChangeReceiver.Visible = false;
                    break;
                case BugStatus.已关闭:
                    liClose.Visible = btnClose.Visible = false;
                    liDo.Visible = btnDo.Visible = false;
                    liChangeReceiver.Visible = btnChangeReceiver.Visible = false;
                    break;
                case BugStatus.处理中:
                    liClose.Visible = btnClose.Visible = false;
                    liDo.Visible = btnDo.Visible = true;
                    liChangeReceiver.Visible = btnChangeReceiver.Visible = true;
                    break;
                case BugStatus.新的:
                case BugStatus.重新打开:
                    liClose.Visible = btnClose.Visible = false;
                    liDo.Visible = btnDo.Visible = true;
                    liChangeReceiver.Visible = btnChangeReceiver.Visible = true;
                    break;

            }

            if (ComeFrom != FromPage.DoList)
            {
                //登记的人可以关闭缺陷
                if (ComeFrom != FromPage.RegList)
                {
                    liClose.Visible = btnClose.Visible = false;
                }
                liDo.Visible = btnDo.Visible = false;
                liChangeReceiver.Visible = btnChangeReceiver.Visible = false;
            }

            rpt.DataSource = info.Pictures;
            rpt.DataBind();

            //if (ComeFrom == FromPage.PrjBugList)
            //{
            //    btnCancel.Attributes.Add("onclick", "history.back();return false;");
            //}
       
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (ComeFrom == FromPage.PrjBugList)
            {
                PrjBugInfo info = _bug.GetInfo(PKID);
                Response.Redirect(ComeFrom.ToString() + ".aspx?IsReturn=true&PrjID=" + this.Encrypt(info.PrjID), true);
            }
            else
            {
                Response.Redirect(ComeFrom.ToString() + ".aspx?IsReturn=true", true);
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            PrjBugInfo info = _bug.GetInfo(PKID);
            info.Status = (int)BugStatus.已关闭;
            _bug.Save(info);
            DispInfo();
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            DispInfo();
        }
    }
}
